//using Android.Health.Connect.DataTypes;
using TodoSQLite.Data;
using TodoSQLite.Models;

namespace TodoSQLite.Views;

[QueryProperty("Item", "Item")]
public partial class TodoItemPage : ContentPage
{
	TodoItem item;
	public TodoItem Item
	{
		get => BindingContext as TodoItem;
		set => BindingContext = value;
	}
    TodoItemDatabase database;
    public TodoItemPage(TodoItemDatabase todoItemDatabase)
    {
        database = todoItemDatabase;
        InitializeComponent();
        Loaded += MainPage_Loaded;
    }
    private void MainPage_Loaded(object sender, EventArgs e)
    {
        entry.Focus();
    }

    async void OnSaveClicked(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(Item.Name))
        {
            await DisplayAlert("Name Required", "Please enter a name for the todo item.", "OK");
            return;
        }
        if (Item.Done)
            await database.DeleteItemAsync(Item);
        else
        {
            await database.SaveItemAsync(Item);
        }
        
        await Shell.Current.GoToAsync("..");
        
    }

    async void OnDeleteClicked(object sender, EventArgs e)
    {
        if (Item.ID == 0)
            return;
        await database.DeleteItemAsync(Item);
        await Shell.Current.GoToAsync("..");
    }

    async void OnReturnClicked(object sender, EventArgs e)
    {
        await Shell.Current.GoToAsync("..");
    }

}